﻿/// <reference path="jquery-1.4.1.js" />
//MPBlog的核心js，提供全局的js功能，以及ajax相关功能

var Namespace = new Object();
Namespace.register = function (path) {
    var arr = path.split(".");
    var ns = "";
    for (var i = 0; i < arr.length; i++) {
        if (i > 0)
            ns += ".";
        ns += arr[i];
        eval("if(typeof(" + ns + ") == 'undefined') " + ns + " = new Object();");
    }
};

//命名空间声明
Namespace.register("MPBlog.ajax.widgetManage.remove");
Namespace.register("MPBlog.ajax.widgetManage.add");

Namespace.register("MPBlog.utils.message");

Namespace.register("MPBlog.func.comment.addReplyBox");

Namespace.register("MPBlog.validate.addWidget");
Namespace.register("MPBlog.validate.addArticle");
Namespace.register("MPBlog.validate.addComment");

/*************************************       MPBlog.ajax      ***************************************************/
MPBlog.ajax.widgetManage.remove = function (element, widgetId) {
    $.ajax({
        type: "post",
        url: "Ajax/WidgetManage.aspx/RemoveWidget",
        contentType: "application/json;charset=utf-8",
        data: "{'widgetId':'" + widgetId + "'}",
        dataType: "json",
        success: function (data) {
            if (data.d == "True") {
                $(element).parent(".widget").remove(); //TODO:要求widget的包装器必须有widget 的class属性
                window.MPBlog.utils.message("删除成功", "success", 3000);
            }
            else {
                window.MPBlog.utils.message("删除失败", "error", 3000);
            }
        },
        error: function (err) {
            window.MPBlog.utils.message("删除失败，错误信息：" + err, "error", 3000);
        }
    });
};


/*************************************       MPBlog.utils     ***************************************************/

//<summary>弹出消息提示</summary>
//msg:要提示的信息
//type: info or success or error
//autoCloseTime:自动关闭时间（毫秒），0为不自动关闭
MPBlog.utils.message = function (msg, type, autoCloseTime) {
    var elementHtml = $("<div class='MPBlogMessage'><table><tr valign='middle'><td>&nbsp;</td><td>" + msg + "</td><td></td></tr></div>");
    var boxHeight = "35px";


    elementHtml.css({
        "position": "fixed",
        "left": "0",
        "top": "0",
        "border-color": "#A2B4EE",
        "color": "#585B66",
        "border-radius": "6px",
        "border": "1px solid",
        "font-size": "13px",
        "padding": "0",
        "width": "99.8%",
        "z-index": "99999", //确保提示框在最顶层
        "height": "0"  //动画之前全部隐藏
    });

    elementHtml.find("table").css({
        "width": "100%",
        "height": boxHeight
    });

    if (type == "info") {
        elementHtml.find("table>tbody>tr>td:eq(0)").css({
            "width": "30px",
            "background": "url('../Images/msg_info.png') no-repeat center center"
        });

        elementHtml.css({
            "background-color": "#DBE3FF"
        });

    }
    else if (type == "success") {
        elementHtml.find("table>tbody>tr>td:eq(0)").css({
            "width": "30px",
            "background": "url('../Images/msg_success.png') no-repeat center center"
        });

        elementHtml.css({
            "background-color": "#D5FFCE"
        });
    }
    else if (type == "error") {
        elementHtml.find("table>tbody>tr>td:eq(0)").css({
            "width": "30px",
            "background": "url('../Images/msg_error.png') no-repeat center center"
        });

        elementHtml.css({
            "background-color": "#FFCECE"
        });
    }

    elementHtml.find("table>tbody>tr>td:eq(1)").css({
        "line-height": boxHeight //确保文字高度居中
    });

    elementHtml.find("table>tbody>tr>td:eq(2)").css({
        "width": "20px",
        "background": "url('../Images/msg_close.png') no-repeat center center",
        "cursor": "pointer"
    });

    //关闭事件
    elementHtml.find("table>tbody>tr>td:eq(2)").click(function () {
        mpBlogMessageClose();
    });

    function mpBlogMessageClose() {
        elementHtml.animate({
            "height": "0"
        }, 200, function () {
            elementHtml.remove();
        });
    }

    $(".MPBlogMessage").remove(); //删除所有已经存在的提示
    $("body").prepend(elementHtml);

    //是否自动关闭提示信息
    if (autoCloseTime > 0) {
        elementHtml.animate({
            "height": "35px"
        }, 200, function () {
            setTimeout(function () {
                mpBlogMessageClose();
            }, autoCloseTime);
        });
    }
    else {
        elementHtml.animate({
            "height": "35px"
        }, 200);
    }

};


/*************************************       MPBlog.validate     ***************************************************/

//检查是否已经选择了要添加的小部件
//dropDownListId:部件选择下拉框
MPBlog.validate.addWidget = function(dropDownListId) {
    var selectedValue = $("#" + dropDownListId).val();
    if (selectedValue == "-添加小部件-") {
        MPBlog.utils.message("请选择需要添加的小部件", "info", 3000);
        return false;
    }
    else {
        return true;
    }
};


MPBlog.validate.addArticle = function (titleId, contentId) {
    var title = $("#" + titleId).val();
    
    //运用FCKEditor的api获得文本值
    var checkContent = FCKeditorAPI.GetInstance(contentId).GetXHTML();
    checkContent = checkContent.replace(/&nbsp;/g, "");
    checkContent = checkContent.replace(/<p><\/p>/g, "");
    checkContent = checkContent.replace(/<br\/>/g, "");
    checkContent = checkContent.replace(/<div><\/div>/g, "");


    if (title == null || title == "") {
        MPBlog.utils.message("请填写文章标题", "error", 3000);
        return false;   
    }
    else if (checkContent == null || checkContent == "") {
        MPBlog.utils.message("请填写文章内容", "error", 3000);
        return false;
    }

    return true;
}

//添加评论验证
//name:评论人姓名文本框ID
//name:EMail文本框ID
//name:评论文本框ID
MPBlog.validate.addComment = function (nameId, emailId, contentId) {
    var name = $("#" + nameId).val();
    var mail = $("#" + emailId).val();
    var content = $("#" + contentId).val();

    if (name == null || name == "") {
        MPBlog.utils.message("请填写您的大名", "error", 5000);
        return false;
    }
    if (mail == null || mail == "") {
        MPBlog.utils.message("不留下联系方式是嘛意思？", "error", 5000);
        return false;
    }
    if (content == null || content == "") {
        MPBlog.utils.message("如果不写评论内容的话...>_<", "error", 5000);
        return false;
    }
    return true;
}


/*************************************       MPBlog.func     ***************************************************/
//添加评论回复输入框
//linkObj:点击的回复链接对象
MPBlog.func.comment.addReplyBox = function (linkObj) {
    alert($(linkObj).parentsUntil(".content").html());
};